<!doctype html>
<html>

	<head>
		<meta charset="utf-8">
		<title>积分充值</title>
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<link href="css/mui.css" rel="stylesheet" />
		<link rel="stylesheet" type="text/css" href="css/common.css" />
		<link rel="stylesheet" type="text/css" href="css/index.css" />
	</head>

	<body class="bg_ff">
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">积分充值</h1>
		</header>
		<div class="mui-content bg_ff ">
			<div class="padding_12 mg_top20">
				<p class="bold color_00">选择充值积分数额</p>
				<ul class=" recharge" id="recharge">
					<!--<li class="recharge_item active">
						<p class="bold color_25 mg_b10">2,000积分</p>
						<h6 class="color_55">￥50.00元</h6>
					</li>-->

				</ul>
				<p class="bold color_00 mg_top20">充值方式</p>
				<ul class=" recharge" id="paystyle">
					<li class="recharge_style" data-type='2'>
						<img src="images/icon_pay.png" />支付宝
					</li>
					<li class="recharge_style" data-type='1'>
						<img src="images/pay_wx.png" />微信
					</li>

				</ul>

				<p class="text-center mg_top20 width90"><a href="javascript:void(0);" class="big_btn" onclick="coderecharge()">立即充值</a></p>
				<p class="text-center mg_top20 width90">
					<!--<a href="javascript:void(0);" class="big_btn" id="rechargebtn">立即充值</a>-->
				</p>
			</div>

		</div>
		<div class="msg-spring"></div>

		<script src="js/mui.js"></script>
		<script src="js/jquery-1.7.1.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="js/index.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript">
			mui.init()
			$('.recharge').on('click', '.recharge_item', function() {
				$(this).addClass('active').siblings().removeClass('active')
			})
			$('.recharge').on('click', '.recharge_style', function() {
				$(this).addClass('active').siblings().removeClass('active')
			})
			//获取积分列表

			function focusUser() {
				$.ajax({
					type: "get",
					url: urlcom + "userCenter/focusUser",
					async: true,
					dataType: 'json',
					success: function(res) {
						if(res.code == 0) {
							var html = '';
							$.each(res.data, function(k, v) {
								html += '<li class="recharge_item " data-money=' + v.money + ' id="' + v.integralSetId + '">' +
									'<p class="bold color_25 mg_b10">' + v.integral + '积分</p>' +
									'<h6 class="color_55">￥' + v.money + '元</h6>' +
									'</li>'
							});
							$('#recharge').html(html)

						}
					}
				})
			}focusUser()
			
			var wxChannel = null; // 微信支付 
			var aliChannel = null; // 支付宝支付
			mui.plusReady(function() {
			
				//获取支付通道
				plus.payment.getChannels(function(channels) {
					channels.forEach(function(k,v){
						if(k.id=='wxpay'){
							wxChannel = k
						}else if(k.id=='alipay'){
							
							aliChannel = k
						}
					})
				}, function(e) {
					alert("获取支付通道失败：" + e.message);
				})		
				
			});
			

			//积分充值
			function coderecharge() {
				var integralSetId = $('#recharge li.active').attr('id')
				var type = $('#paystyle li.active').attr('data-type')
				if(!integralSetId) {
					msg('请选择积分')
					return false;
				}
				if(!type) {
					msg('请选择支付方式')
					return false;
				}
				$.ajax({
					type: "post",
					url: urlcom + "pay/getIntegralPayInfo",
					async: true,
					dataType: 'json',
					data: {
						userId: localStorage.getItem('userId'),
						integralSetId: integralSetId,
						payType: type
					},
					success: function(res) {
						if(res.code == 0) {
							if(type == 2) {
								plus.payment.request(aliChannel, res.data.orderInfo, function(result) {
									plus.nativeUI.alert('支付成功!', function() {
										mui.back();
									}, '充值');
								}, function(e) {
									plus.nativeUI.alert('支付宝支付失败');
								});
							} else if(type == 1) {
								plus.payment.request(wxChannel, {
									"appid":res.data.appid,
									"partnerid":res.data.partnerId,
									"package":res.data.package,
									"prepayid":res.data.prepayId,
									"noncestr":res.data.nonceStr,
									"timestamp":res.data.timeStamp,
									"sign":res.data.sign,
									
								}, function(result) {
									plus.nativeUI.alert('支付成功!', function() {
										mui.back();
									}, '充值');
								}, function(e) {
									plus.nativeUI.alert(JSON.stringify(e));
								});
							}
							

						} else {
							msg(res.msg)
						}
					}
				})
			}
			
			
			
			
		</script>
	</body>

</html>